package com.club.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.club.entity.Activity;
import com.club.mapper.ActivityMapper;
import com.club.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.time.LocalDateTime;

/**
 * 活动服务实现类
 */
@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements ActivityService {

    @Autowired
    private ActivityMapper activityMapper;

    @Override
    public boolean createActivity(Activity activity) {
        activity.setCreateTime(LocalDateTime.now());
        activity.setUpdateTime(LocalDateTime.now());
        activity.setStatus(0); // 默认待审核状态
        return save(activity);
    }

    @Override
    public boolean updateActivity(Activity activity) {
        activity.setUpdateTime(LocalDateTime.now());
        return updateById(activity);
    }

    @Override
    public boolean deleteActivity(Long id) {
        return removeById(id);
    }

    @Override
    public Activity getActivityById(Long id) {
        return getById(id);
    }

    // 暂不实现这个方法，因为接口中没有定义

    @Override
    public boolean approveActivity(Long id, Integer approvalStatus, Long approverId, String reason) {
        Activity activity = getById(id);
        if (activity != null) {
            activity.setApprovalStatus(approvalStatus);
            activity.setApproverId(approverId);
            activity.setApprovalTime(LocalDateTime.now());
            activity.setUpdateTime(LocalDateTime.now());
            return updateById(activity);
        }
        return false;
    }

    @Override
    public List<Activity> getActivitiesByClubId(Long clubId) {
        // 根据社团ID查询活动
        return activityMapper.selectByClubId(clubId);
    }

    @Override
    public List<Activity> getAllActivities(Integer status) {
        // 查询所有活动
        return list();
    }

    @Override
    public boolean incrementParticipantCount(Long id) {
        Activity activity = getById(id);
        if (activity != null) {
            activity.setCurrentParticipants(activity.getCurrentParticipants() + 1);
            return updateById(activity);
        }
        return false;
    }

    @Override
    public boolean decrementParticipantCount(Long id) {
        Activity activity = getById(id);
        if (activity != null && activity.getCurrentParticipants() > 0) {
            activity.setCurrentParticipants(activity.getCurrentParticipants() - 1);
            return updateById(activity);
        }
        return false;
    }

    @Override
    public boolean updateActivityStatus(Long id, Integer status) {
        Activity activity = getById(id);
        if (activity != null) {
            activity.setStatus(status);
            activity.setUpdateTime(LocalDateTime.now());
            return updateById(activity);
        }
        return false;
    }
}